package com.rcw.swiperefreshrecyclerview;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ruancw on 2018/3/29.
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<ViewHolder>{
    protected Context context;
    protected  int layoutId;
    protected List<T> beans;
    protected LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;

    protected final static int GET = 1;
    protected final static int POST = 2;
    /**
     * 设置是否显示EmptyView
     */
    protected boolean showEmptyView = false;
    /**
     * 标识是否显示EmptyView
     */
    protected boolean isShowEmptyView = false;
    /**
     * 全部加载完毕是否显示底部View
     */
    protected boolean isShowEndView = false;
    /**
     * 是否添加了显示底部View的数据
     */
    protected boolean isAddShowEndViewData = false;

    public BaseAdapter(Context context, int layoutId, List<T> beans) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.layoutId = layoutId;
        this.beans = beans;
        if(this.beans == null){
            this.beans = new ArrayList<>();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return ViewHolder.get(context,parent,layoutId);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        convert(holder,beans.get(position),position);
        if (onItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.setOnClickListener(holder.itemView,position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onItemClickListener.setOnLongClickListener(holder.itemView,position);
                    //返回true,为了防止影响onClick事件
                    return true;
                }
            });
        }

    }
    //2018/8/31添加position参数，用于某些需要隔行变色的地方
    public abstract void convert(ViewHolder holder, T t,int position);

    @Override
    public int getItemCount() {
        return beans.size();
    }

    public List<T> getDatas(){
        return beans;
    }

    public void setDatas(List<T> beans){
        this.beans = beans;
        if(this.beans == null){
            this.beans = new ArrayList<>();
            isShowEmptyView = true;
        }else{
            isShowEmptyView = false;
        }
        notifyDataSetChanged();
    }

    public void insertDataAtTop(T newData){
        if(beans!=null){
            beans.add(0,newData);
        }else{
            beans =  new ArrayList<>();
            beans.add(newData);
        }
        notifyDataSetChanged();
    }

    public void addDatas(List<T> newDatas){
        if(this.beans == null){
            this.beans = new ArrayList<>();
        }
        int last = beans.size();
        this.beans.addAll(newDatas);
        notifyItemInserted(last + 1);
    }
    public void addData(T newData){
        if(this.beans == null){
            this.beans = new ArrayList<>();
        }
        this.beans.add(newData);
        notifyDataSetChanged();
    }
    public void addData(T newData,int index){
        if(this.beans == null){
            this.beans = new ArrayList<>();
        }
        this.beans.add(index,newData);
        notifyDataSetChanged();
    }

    public void setShowEmptyView(boolean showEmptyView) {
        this.showEmptyView = showEmptyView;
    }

    public void setIsShowEmptyView(boolean show) {
        if(!showEmptyView) return;
        isShowEmptyView = show;
        if(isShowEmptyView){
            this.beans.clear();
            insertDataAtTop((T) new Object());
        }

    }

    public boolean isShowEndView() {
        return isShowEndView;
    }

    public void setShowEndView(boolean showEndView) {
        isShowEndView = showEndView;
    }

    public void addEndViewData(){
        if(isAddShowEndViewData || !isShowEndView) return;
        isAddShowEndViewData = true;
        if(beans==null){
            beans = new ArrayList<>();
        }
        beans.add((T) new Object());
    }

    public void removeEndViewData(){
        if(!isAddShowEndViewData || !isShowEndView) return;
        isAddShowEndViewData = false;
        if(beans!=null && beans.size()>0){
            beans.remove(beans.size()-1);
        }
    }


    public void removeItem(int position){
        if(beans != null && beans.size() > position){
            beans.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void clearDatas(){
        this.beans.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
}
